﻿Imports System.Data.SqlClient

Public Class frmCaseEntry

    Public _MainID As Long
    Public _CaseID As Long

    Public _MainCaseID_ As Long
    Public _CaseID_ As Long
    Public _isViewCase As Boolean = False
    Public _isAddCase As Boolean = False

    Dim isInserted As Boolean = False

    Private Sub frmCaseEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reset()
        getCaseCategory()
        getCourt()
        getHeaderGrid_Judge()
        getHeaderGrid_CourtClerk()
        getHeaderGrid_Plaintiff()
        getHeaderGrid_Defendant()
        getHeaderGrid_ThirdParty()
        getHeaderGrid_LawyerPlaintiff()
        getHeaderGrid_LawyerDefendant()
        getHeaderGrid_LawyerThirdParty()
        getHeaderGrid_FileCasePlaintiff()
        getHeaderGrid_FileCaseDefendant()
        getHeaderGrid_FileCaseThirdParty()
        getHeaderGrid_DescriptionCasePlaintiff()
        getHeaderGrid_DescriptionCaseDefendant()
        getHeaderGrid_DescriptionCaseThirdParty()

        If _MainCaseID_ <> 0 Then
            If _isViewCase Then
                Dim dt As New DataTable
                Dim oCase As New cCase
                dt = oCase.select_Case_byID(_CaseID_)
                If dt.Rows(0)("IsFinish") = True Then
                    EnableCase_All()
                Else
                    EnableCase()
                End If

                getTextbox_MainCase()
                getTextbox_Case()
                getTextbox_Judge()
                getTextbox_CourtClerk()
                getTextbox_Plaintiff()
                getTextbox_Defendant()
                getTextbox_ThirdParty()
                getTextbox_LawyerPlaintiff()
                getTextbox_LawyerDefendant()
                getTextbox_LawyerThirdParty()
                getTextbox_FileCasePlaintiff()
                getTextbox_FileCaseDefendant()
                getTextbox_FileCaseThirdParty()
                getTextbox_DescriptionCasePlaintiff()
                getTextbox_DescriptionCaseDefendant()
                getTextbox_DescriptionCaseThirdParty()

                ClearSelection()
            ElseIf _isAddCase Then
                getTextbox_MainCase()
                insert_Case_ID()
            End If
        Else
            insert_MainCase_ID()
            insert_Case_ID()
        End If
    End Sub

    Protected Sub getTextbox_MainCase()
        Dim oMainCase As New cMainCase
        Dim dt As New DataTable
        dt = oMainCase.select_MainCase_byID(_MainCaseID_)
        If dt.Rows.Count > 0 Then
            txtMainCaseName.Text = dt.Rows(0)("MainCaseName")
            cboCaseCategory.SelectedValue = dt.Rows(0)("CaseCategoryID")
        End If
    End Sub

    Protected Sub getTextbox_Case()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_byID(_CaseID_)
        If dt.Rows.Count > 0 Then
            txtCaseNo.Text = dt.Rows(0)("CaseNo")
            cboCourt.SelectedValue = dt.Rows(0)("CourtID")
            txtTel.Text = dt.Rows(0)("Tel")
            txtCaseFee.Text = dt.Rows(0)("CaseFee")
            txtSuccessfulCase.Text = dt.Rows(0)("SuccessfulCase")
            dtpStartDate.Value = dt.Rows(0)("StartDate")
            If dt.Rows(0)("IsFinish") Then
                txtTotalDays.Text = dt.Rows(0)("TotalDays")
            Else
                txtTotalDays.Text = DateDiff(DateInterval.Day, Date.Parse(dt.Rows(0)("StartDate")), Now)
            End If
        End If
    End Sub

    Protected Sub getTextbox_Judge()
        dgvJudge.Rows.Clear()
        Dim oJudge As New cJudge
        Dim dt As New DataTable
        Dim dr As DataRow
        dt = oJudge.select_Judge_byCaseID(_CaseID_)
        For Each dr In dt.Rows
            dgvJudge.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_CourtClerk()
        dgvCourtClerk.Rows.Clear()
        Dim oCourtClerk As New cCourtClerk
        Dim dt As New DataTable
        dt = oCourtClerk.select_CourtClerk_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvCourtClerk.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_Plaintiff()
        dgvPlaintiff.Rows.Clear()
        Dim oPlaintiff As New cPlaintiff
        Dim dt As New DataTable
        Dim dr As DataRow
        dt = oPlaintiff.select_Plaintiff_byCaseID(_CaseID_)
        For Each dr In dt.Rows
            dgvPlaintiff.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_Defendant()
        dgvDefendant.Rows.Clear()
        Dim oDefendant As New cDefendant
        Dim dt As New DataTable
        dt = oDefendant.select_Defendant_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvDefendant.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_ThirdParty()
        dgvThirdParty.Rows.Clear()
        Dim oThirdParty As New cThirdParty
        Dim dt As New DataTable
        dt = oThirdParty.select_ThirdParty_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvThirdParty.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_LawyerPlaintiff()
        dgvLawyerPlaintiff.Rows.Clear()
        Dim oLawyerPlaintiff As New cLawyerPlaintiff
        Dim dt As New DataTable
        dt = oLawyerPlaintiff.select_LawyerPlaintiff_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvLawyerPlaintiff.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_LawyerDefendant()
        dgvLawyerDefendant.Rows.Clear()
        Dim oLawyerDefendant As New cLawyerDefendant
        Dim dt As New DataTable
        dt = oLawyerDefendant.select_LawyerDefendant_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvLawyerDefendant.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_LawyerThirdParty()
        dgvLawyerThirdParty.Rows.Clear()
        Dim oLawyerThirdParty As New cLawyerThirdParty
        Dim dt As New DataTable
        dt = oLawyerThirdParty.select_LawyerThirdParty_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvLawyerThirdParty.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_FileCasePlaintiff()
        dgvFilePlaintiff.Rows.Clear()
        Dim oFile As New cFile
        Dim dt As New DataTable
        dt = oFile.select_File_CasePlaintiff_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvFilePlaintiff.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_FileCaseDefendant()
        dgvFileDefendant.Rows.Clear()
        Dim oFile As New cFile
        Dim dt As New DataTable
        dt = oFile.select_File_CaseDefendant_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvFileDefendant.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_FileCaseThirdParty()
        dgvFileThirdParty.Rows.Clear()
        Dim oFile As New cFile
        Dim dt As New DataTable
        dt = oFile.select_File_CaseThirdParty_byCaseID(_CaseID_)
        Dim dr As DataRow
        For Each dr In dt.Rows
            dgvFileThirdParty.Rows.Add(dr(0), dr(1))
        Next
    End Sub

    Protected Sub getTextbox_DescriptionCasePlaintiff()
        dgvDescriptionPlaintiff.Rows.Clear()
        Dim oDescription As New cDescription
        Dim dt As New DataTable
        dt = oDescription.select_Description_CasePlaintiff_byCaseID(_CaseID_)
        Dim dr As DataRow
        Dim richtextbox As New RichTextBox
        Dim Description As String
        For Each dr In dt.Rows
            richtextbox.Rtf = dr(1)
            Description = richtextbox.Text
            dgvDescriptionPlaintiff.Rows.Add(dr(0), Description)
        Next
    End Sub

    Protected Sub getTextbox_DescriptionCaseDefendant()
        dgvDescriptionDefendant.Rows.Clear()
        Dim oDescription As New cDescription
        Dim dt As New DataTable
        dt = oDescription.select_Description_CaseDefendant_byCaseID(_CaseID_)
        Dim dr As DataRow
        Dim richtextbox As New RichTextBox
        Dim Description As String
        For Each dr In dt.Rows
            richtextbox.Rtf = dr(1)
            Description = richtextbox.Text
            dgvDescriptionDefendant.Rows.Add(dr(0), Description)
        Next
    End Sub

    Protected Sub getTextbox_DescriptionCaseThirdParty()
        dgvDescriptionThirdParty.Rows.Clear()
        Dim oDescription As New cDescription
        Dim dt As New DataTable
        dt = oDescription.select_Description_CaseThirdParty_byCaseID(_CaseID_)
        Dim dr As DataRow
        Dim richtextbox As New RichTextBox
        Dim Description As String
        For Each dr In dt.Rows
            richtextbox.Rtf = dr(1)
            Description = richtextbox.Text
            dgvDescriptionThirdParty.Rows.Add(dr(0), Description)
        Next
    End Sub

    Protected Sub getHeaderGrid_Judge()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះចៅក្រម"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvJudge.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvJudge.Columns(i).HeaderText = HeaderText(i)
                dgvJudge.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvJudge.Columns(i).HeaderText = HeaderText(i)
                dgvJudge.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvJudge.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_CourtClerk()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះក្រឡាបញ្ជី"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvCourtClerk.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvCourtClerk.Columns(i).HeaderText = HeaderText(i)
                dgvCourtClerk.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvCourtClerk.Columns(i).HeaderText = HeaderText(i)
                dgvCourtClerk.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvCourtClerk.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_Plaintiff()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvPlaintiff.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvPlaintiff.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvPlaintiff.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvPlaintiff.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_Defendant()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvDefendant.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvDefendant.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvDefendant.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvDefendant.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_ThirdParty()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvThirdParty.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvThirdParty.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvThirdParty.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvThirdParty.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_LawyerPlaintiff()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvLawyerPlaintiff.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvLawyerPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerPlaintiff.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvLawyerPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerPlaintiff.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvLawyerPlaintiff.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_LawyerDefendant()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvLawyerDefendant.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvLawyerDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerDefendant.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvLawyerDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerDefendant.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvLawyerDefendant.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_LawyerThirdParty()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvLawyerThirdParty.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvLawyerThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerThirdParty.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvLawyerThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvLawyerThirdParty.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvLawyerThirdParty.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_FileCasePlaintiff()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvFilePlaintiff.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvFilePlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvFilePlaintiff.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvFilePlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvFilePlaintiff.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvFilePlaintiff.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_FileCaseDefendant()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvFileDefendant.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvFileDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvFileDefendant.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvFileDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvFileDefendant.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvFileDefendant.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_FileCaseThirdParty()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvFileThirdParty.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvFileThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvFileThirdParty.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvFileThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvFileThirdParty.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvFileThirdParty.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_DescriptionCasePlaintiff()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvDescriptionPlaintiff.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvDescriptionPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionPlaintiff.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvDescriptionPlaintiff.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionPlaintiff.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvDescriptionPlaintiff.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_DescriptionCaseDefendant()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvDescriptionDefendant.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvDescriptionDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionDefendant.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvDescriptionDefendant.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionDefendant.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvDescriptionDefendant.Columns(0).Visible = False
    End Sub

    Protected Sub getHeaderGrid_DescriptionCaseThirdParty()
        Dim HeaderText As String() = {"លេខរៀង", "ឈ្មោះ"}
        Dim HeaderWidth As Integer() = {100, 100}
        Dim i As Integer
        dgvDescriptionThirdParty.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            If i = HeaderText.Count - 1 Then
                dgvDescriptionThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionThirdParty.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnsMode.Fill
            Else
                dgvDescriptionThirdParty.Columns(i).HeaderText = HeaderText(i)
                dgvDescriptionThirdParty.Columns(i).Width = HeaderWidth(i)
            End If
        Next
        dgvDescriptionThirdParty.Columns(0).Visible = False
    End Sub

    Protected Sub insert_MainCase_ID()
        Dim oMainCase As New cMainCase
        Try
            _MainID = getMainID()
            oMainCase.insert_MainCase_ID(_MainID)
        Catch ex As Exception
            MessageBox.Show("កម្មវិធីនេះមិនអាចបញ្ចូលរឿងក្តីបានទេ", "សូមអភ័យទោស", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Protected Sub insert_Case_ID()
        Dim oCase As New cCase
        Try
            _CaseID = getCaseID()
            oCase.insert_Case_ID(_CaseID)
        Catch ex As Exception
            MessageBox.Show("កម្មវិធីនេះមិនអាចបញ្ចូលរឿងក្តីបានទេ", "សូមអភ័យទោស", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub EnableCase()
        txtMainCaseName.Enabled = False
        cboCaseCategory.Enabled = False
        txtCaseNo.Enabled = False
        cboCourt.Enabled = False
        txtTel.Enabled = False
        txtCaseFee.Enabled = False
        txtSuccessfulCase.Enabled = False
        dtpStartDate.Enabled = False
        txtTotalDays.Enabled = False
        btnSave.Enabled = False
        btnShow.Enabled = False
    End Sub

    Public Sub EnableMainCase()
        txtMainCaseName.Enabled = False
        cboCaseCategory.Enabled = False
    End Sub

    Public Sub EnableCase_All()
        txtMainCaseName.Enabled = False
        cboCaseCategory.Enabled = False
        txtCaseNo.Enabled = False
        cboCourt.Enabled = False
        txtTel.Enabled = False
        txtCaseFee.Enabled = False
        txtSuccessfulCase.Enabled = False
        dtpStartDate.Enabled = False
        txtTotalDays.Enabled = False
        btnAddJudge.Enabled = False
        btnAddCourtClerk.Enabled = False
        AddPlaintiff.Enabled = False
        AddDefendant.Enabled = False
        AddThirdParty.Enabled = False
        AddLawyerPlaintiff.Enabled = False
        AddLawyerDefendant.Enabled = False
        AddLawyerThirdParty.Enabled = False
        AddFilePlaintiff.Enabled = False
        AddFileDefendant.Enabled = False
        AddFileThirdParty.Enabled = False
        AddDescriptionPlaintiff.Enabled = False
        AddDescriptionDefendant.Enabled = False
        AddDescriptionThirdParty.Enabled = False
        btnSave.Enabled = False
        btnShow.Enabled = False
        btnFinishCase.Enabled = False
    End Sub

    Protected Function getMainID() As Long
        Dim ID As Long
        Dim oControl As New cControl
        ID = oControl.getID("MainCaseID", "tblMainCase")
        Return ID
    End Function

    Protected Function getCaseID() As Long
        Dim ID As Long
        Dim oControl As New cControl
        ID = oControl.getID("CaseID", "tblCase")
        Return ID
    End Function

    Public Sub getJudge()
        If _isViewCase Then
            getTextbox_Judge()
        Else
            dgvJudge.Rows.Clear()
            Dim oJudge As New cJudge
            Dim dt As New DataTable
            dt = oJudge.select_Judge_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvJudge.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getCourtClerk()
        If _isViewCase Then
            getTextbox_CourtClerk()
        Else
            dgvCourtClerk.Rows.Clear()
            Dim oCourtClerk As New cCourtClerk
            Dim dt As New DataTable
            dt = oCourtClerk.select_CourtClerk_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvCourtClerk.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getPlaintiff()
        If _isViewCase Then
            getTextbox_Plaintiff()
        Else
            dgvPlaintiff.Rows.Clear()
            Dim oPlaintiff As New cPlaintiff
            Dim dt As New DataTable
            dt = oPlaintiff.select_Plaintiff_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvPlaintiff.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getDefendant()
        If _isViewCase Then
            getTextbox_Defendant()
        Else
            dgvDefendant.Rows.Clear()
            Dim oDefendant As New cDefendant
            Dim dt As New DataTable
            dt = oDefendant.select_Defendant_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvDefendant.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getThirdParty()
        If _isViewCase Then
            getTextbox_ThirdParty()
        Else
            dgvThirdParty.Rows.Clear()
            Dim oThirdParty As New cThirdParty
            Dim dt As New DataTable
            dt = oThirdParty.select_ThirdParty_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvThirdParty.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getLawyerPlaintiff()
        If _isViewCase Then
            getTextbox_LawyerPlaintiff()
        Else
            dgvLawyerPlaintiff.Rows.Clear()
            Dim oLawyerPlaintiff As New cLawyerPlaintiff
            Dim dt As New DataTable
            dt = oLawyerPlaintiff.select_LawyerPlaintiff_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvLawyerPlaintiff.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getLawyerDefendant()
        If _isViewCase Then
            getTextbox_LawyerDefendant()
        Else
            dgvLawyerDefendant.Rows.Clear()
            Dim oLawyerDefendant As New cLawyerDefendant
            Dim dt As New DataTable
            dt = oLawyerDefendant.select_LawyerDefendant_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvLawyerDefendant.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getLawyerThirdParty()
        If _isViewCase Then
            getTextbox_LawyerThirdParty()
        Else
            dgvLawyerThirdParty.Rows.Clear()
            Dim oLawyerThirdParty As New cLawyerThirdParty
            Dim dt As New DataTable
            dt = oLawyerThirdParty.select_LawyerThirdParty_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvLawyerThirdParty.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getFileCasePlaintiff()
        If _isViewCase Then
            getTextbox_FileCasePlaintiff()
        Else
            dgvFilePlaintiff.Rows.Clear()
            Dim oFile As New cFile
            Dim dt As New DataTable
            dt = oFile.select_File_CasePlaintiff_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvFilePlaintiff.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getFileCaseDefendant()
        If _isViewCase Then
            getTextbox_FileCaseDefendant()
        Else
            dgvFileDefendant.Rows.Clear()
            Dim oFile As New cFile
            Dim dt As New DataTable
            dt = oFile.select_File_CaseDefendant_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvFileDefendant.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getFileCaseThirdParty()
        If _isViewCase Then
            getTextbox_FileCaseThirdParty()
        Else
            dgvFileThirdParty.Rows.Clear()
            Dim oFile As New cFile
            Dim dt As New DataTable
            dt = oFile.select_File_CaseThirdParty_byCaseID(_CaseID)
            Dim dr As DataRow
            For Each dr In dt.Rows
                dgvFileThirdParty.Rows.Add(dr(0), dr(1))
            Next
        End If
    End Sub

    Public Sub getDescription_CasePalintiff()
        If _isViewCase Then
            getTextbox_DescriptionCasePlaintiff()
        Else
            dgvDescriptionPlaintiff.Rows.Clear()
            Dim oDescription As New cDescription
            Dim dt As New DataTable
            dt = oDescription.select_Description_CasePlaintiff_byCaseID(_CaseID)
            Dim dr As DataRow
            Dim richtextbox As New RichTextBox
            Dim Description As String
            For Each dr In dt.Rows
                richtextbox.Rtf = dr(1)
                Description = richtextbox.Text
                dgvDescriptionPlaintiff.Rows.Add(dr(0), Description)
            Next
        End If
    End Sub

    Public Sub getDescription_CaseDefendant()
        If _isViewCase Then
            getTextbox_DescriptionCaseDefendant()
        Else
            dgvDescriptionDefendant.Rows.Clear()
            Dim oDescription As New cDescription
            Dim dt As New DataTable
            dt = oDescription.select_Description_CaseDefendant_byCaseID(_CaseID)
            Dim dr As DataRow
            Dim richtextbox As New RichTextBox
            Dim Description As String
            For Each dr In dt.Rows
                richtextbox.Rtf = dr(1)
                Description = richtextbox.Text
                dgvDescriptionDefendant.Rows.Add(dr(0), Description)
            Next
        End If
    End Sub

    Public Sub getDescription_CaseThirdParty()
        If _isViewCase Then
            getTextbox_DescriptionCaseThirdParty()
        Else
            dgvDescriptionThirdParty.Rows.Clear()
            Dim oDescription As New cDescription
            Dim dt As New DataTable
            dt = oDescription.select_Description_CaseThirdParty_byCaseID(_CaseID)
            Dim dr As DataRow
            Dim richtextbox As New RichTextBox
            Dim Description As String
            For Each dr In dt.Rows
                richtextbox.Rtf = dr(1)
                Description = richtextbox.Text
                dgvDescriptionThirdParty.Rows.Add(dr(0), Description)
            Next
        End If
    End Sub

    Protected Sub getCaseCategory()
        Dim oc As New cControl
        oc.getCombo(cboCaseCategory, "SELECT CaseCategoryID,CaseCategoryName,Description FROM tblCaseCategory WHERE Active=1", "CaseCategoryName", "CaseCategoryID")
    End Sub

    Protected Sub getCourt()
        Dim oc As New cControl
        oc.getCombo(cboCourt, "SELECT CourtID,CourtName FROM tblCourt WHERE Active=1", "CourtName", "CourtID")
    End Sub

    Protected Sub reset()
        txtMainCaseName.Clear()
        If cboCaseCategory.Items.Count > 0 Then
            cboCaseCategory.SelectedIndex = 0
        Else
            cboCaseCategory.SelectedIndex = -1
        End If
        txtCaseNo.Clear()
        If cboCourt.Items.Count > 0 Then
            cboCourt.SelectedIndex = 0
        Else
            cboCourt.SelectedIndex = -1
        End If
        txtTel.Clear()
        txtCaseFee.Clear()
        txtSuccessfulCase.Clear()
        dtpStartDate.Value = Now
        txtTotalDays.Clear()
        dgvJudge.Rows.Clear()
        dgvCourtClerk.Rows.Clear()
        dgvPlaintiff.Rows.Clear()
        dgvLawyerPlaintiff.Rows.Clear()
        dgvFilePlaintiff.Rows.Clear()
        dgvDescriptionPlaintiff.Rows.Clear()
        dgvDefendant.Rows.Clear()
        dgvLawyerDefendant.Rows.Clear()
        dgvFileDefendant.Rows.Clear()
        dgvDescriptionDefendant.Rows.Clear()
        dgvThirdParty.Rows.Clear()
        dgvLawyerThirdParty.Rows.Clear()
        dgvFileThirdParty.Rows.Clear()
        dgvDescriptionThirdParty.Rows.Clear()
        txtMainCaseName.Focus()
    End Sub

    Protected Sub ClearSelection()
        dgvJudge.ClearSelection()
        dgvCourtClerk.ClearSelection()
        dgvPlaintiff.ClearSelection()
        dgvDefendant.ClearSelection()
        dgvThirdParty.ClearSelection()
        dgvLawyerPlaintiff.ClearSelection()
        dgvLawyerDefendant.ClearSelection()
        dgvLawyerThirdParty.ClearSelection()
        dgvFilePlaintiff.ClearSelection()
        dgvFileDefendant.ClearSelection()
        dgvFileThirdParty.ClearSelection()
        dgvDescriptionPlaintiff.ClearSelection()
        dgvDescriptionDefendant.ClearSelection()
        dgvDescriptionThirdParty.ClearSelection()
    End Sub

    Private Sub frmCaseEntry_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If isInserted = False Then
            delete_MainCase()
            delete_Case()
            delete_Judge()
            delete_CourtClerk()
            delete_Plaintiff()
            delete_Defendant()
            delete_ThirdParty()
            delete_LawyerPlaintiff()
            delete_LawyerDefendant()
            delete_LawyerThirdParty()
            delete_File_CasePlaintiff()
            delete_File_CaseDefendant()
            delete_File_CaseThirdParty()
            delete_Description_CasePlaintiff()
            delete_Description_CaseDefendant()
            delete_Description_CaseThirdParty()
        End If
    End Sub

    Protected Sub delete_MainCase()
        If _isAddCase Then
            'No Event
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oMainCase As New cMainCase
            oMainCase.delete_MainCase_ID(_MainID)
        End If
    End Sub

    Protected Sub delete_Case()
        If _isAddCase Then
            Dim oCase As New cCase
            oCase.delete_Case_ID(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oCase As New cCase
            oCase.delete_Case_ID(_CaseID)
        End If
    End Sub

    Protected Sub delete_Judge()
        If _isAddCase Then
            Dim oJudge As New cJudge
            oJudge.delelte_Judge(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oJudge As New cJudge
            oJudge.delelte_Judge(_CaseID)
        End If
    End Sub

    Protected Sub delete_CourtClerk()
        If _isAddCase Then
            Dim oCourtClerk As New cCourtClerk
            oCourtClerk.delelte_CourtClerk(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oCourtClerk As New cCourtClerk
            oCourtClerk.delelte_CourtClerk(_CaseID)
        End If
    End Sub

    Protected Sub delete_Plaintiff()
        If _isAddCase Then
            Dim oPlaintiff As New cPlaintiff
            oPlaintiff.delelte_Plaintiff(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oPlaintiff As New cPlaintiff
            oPlaintiff.delelte_Plaintiff(_CaseID)
        End If
    End Sub

    Protected Sub delete_Defendant()
        If _isAddCase Then
            Dim oDefendant As New cDefendant
            oDefendant.delelte_Defendant(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oDefendant As New cDefendant
            oDefendant.delelte_Defendant(_CaseID)
        End If
    End Sub

    Protected Sub delete_ThirdParty()
        If _isAddCase Then
            Dim oThirdParty As New cThirdParty
            oThirdParty.delelte_ThirdParty(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oThirdParty As New cThirdParty
            oThirdParty.delelte_ThirdParty(_CaseID)
        End If
    End Sub

    Protected Sub delete_LawyerPlaintiff()
        If _isAddCase Then
            Dim oLawyerPlaintiff As New cLawyerPlaintiff
            oLawyerPlaintiff.delelte_LawyerPlaintiff(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oLawyerPlaintiff As New cLawyerPlaintiff
            oLawyerPlaintiff.delelte_LawyerPlaintiff(_CaseID)
        End If
    End Sub

    Protected Sub delete_LawyerDefendant()
        If _isAddCase Then
            Dim oLawyerDefendant As New cLawyerDefendant
            oLawyerDefendant.delelte_LawyerDefendant(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oLawyerDefendant As New cLawyerDefendant
            oLawyerDefendant.delelte_LawyerDefendant(_CaseID)
        End If
    End Sub

    Protected Sub delete_LawyerThirdParty()
        If _isAddCase Then
            Dim oLawyerThirdParty As New cLawyerThirdParty
            oLawyerThirdParty.delelte_LawyerThirdParty(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oLawyerThirdParty As New cLawyerThirdParty
            oLawyerThirdParty.delelte_LawyerThirdParty(_CaseID)
        End If
    End Sub

    Protected Sub delete_File_CasePlaintiff()
        If _isAddCase Then
            Dim oFile As New cFile
            oFile.delete_File_CasePlaintiff(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oFile As New cFile
            oFile.delete_File_CasePlaintiff(_CaseID)
        End If
    End Sub

    Protected Sub delete_File_CaseDefendant()
        If _isAddCase Then
            Dim oFile As New cFile
            oFile.delete_File_CaseDefendant(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oFile As New cFile
            oFile.delete_File_CaseDefendant(_CaseID)
        End If
    End Sub

    Protected Sub delete_File_CaseThirdParty()
        If _isAddCase Then
            Dim oFile As New cFile
            oFile.delete_File_CaseThirdParty(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oFile As New cFile
            oFile.delete_File_CaseThirdParty(_CaseID)
        End If
    End Sub

    Protected Sub delete_Description_CasePlaintiff()
        If _isAddCase Then
            Dim oDescription As New cDescription
            oDescription.delete_Description_CasePlaintiff(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oDescription As New cDescription
            oDescription.delete_Description_CasePlaintiff(_CaseID)
        End If
    End Sub

    Protected Sub delete_Description_CaseDefendant()
        If _isAddCase Then
            Dim oDescription As New cDescription
            oDescription.delete_Description_CaseDefendant(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oDescription As New cDescription
            oDescription.delete_Description_CaseDefendant(_CaseID)
        End If
    End Sub

    Protected Sub delete_Description_CaseThirdParty()
        If _isAddCase Then
            Dim oDescription As New cDescription
            oDescription.delete_Description_CaseThirdParty(_CaseID)
        ElseIf _isViewCase Then
            'No Event
        Else
            Dim oDescription As New cDescription
            oDescription.delete_Description_CaseThirdParty(_CaseID)
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtMainCaseName.Text) And oc.checknull(cboCaseCategory.Text) And oc.checknull(txtCaseNo.Text) And oc.checknull(cboCourt.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtCaseFee.Text) And oc.checknull(txtSuccessfulCase.Text) And oc.checknull(dtpStartDate.Value) And dgvJudge.Rows.Count > 0 And dgvCourtClerk.Rows.Count > 0 Then
            If dgvPlaintiff.Rows.Count > 0 And dgvLawyerPlaintiff.Rows.Count > 0 And dgvDefendant.Rows.Count > 0 And dgvLawyerDefendant.Rows.Count > 0 And dgvThirdParty.Rows.Count > 0 And dgvLawyerThirdParty.Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If _isAddCase Then
            inserted_AddCase()
        Else
            inserted_New()
        End If
    End Sub

    Protected Sub inserted_New()
        If checknull() Then
            Dim oCase As New cCase
            If oCase.insert_Case(_MainID, txtMainCaseName.Text.Trim, cboCaseCategory.SelectedValue, frmMain._LawyerID, frmMain._User, _CaseID, txtCaseNo.Text.Trim, cboCourt.SelectedValue, txtTel.Text.Trim, txtCaseFee.Text.Trim, txtSuccessfulCase.Text.Trim, dtpStartDate.Value) Then
                isInserted = True
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCase.getMainCase()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub inserted_AddCase()
        If checknull() Then
            Dim ocase As New cCase
            If ocase.insert_Case(_MainCaseID_, txtMainCaseName.Text.Trim, cboCaseCategory.SelectedValue, frmMain._LawyerID, frmMain._User, _CaseID, txtCaseNo.Text.Trim, cboCourt.SelectedValue, txtTel.Text.Trim, txtCaseFee.Text.Trim, txtSuccessfulCase.Text.Trim, dtpStartDate.Value) Then
                isInserted = True
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCase.getMainCase()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnAddJudge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddJudge.Click
        If _isViewCase Then
            frmJudge._CaseID = _CaseID_
            frmJudge.ShowDialog()
        Else
            frmJudge._CaseID = _CaseID
            frmJudge.ShowDialog()
        End If
    End Sub

    Private Sub btnAddCourtClerk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCourtClerk.Click
        If _isViewCase Then
            frmCourtClerk._CaseID = _CaseID_
            frmCourtClerk.ShowDialog()
        Else
            frmCourtClerk._CaseID = _CaseID
            frmCourtClerk.ShowDialog()
        End If
    End Sub

    Private Sub AddPlaintiff_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddPlaintiff.LinkClicked
        If _isViewCase Then
            frmPlaintiffEntry._CaseID = _CaseID_
            frmPlaintiffEntry.ShowDialog()
        Else
            frmPlaintiffEntry._CaseID = _CaseID
            frmPlaintiffEntry.ShowDialog()
        End If
    End Sub

    Private Sub AddDefendant_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddDefendant.LinkClicked
        If _isViewCase Then
            frmDefendantEntry._CaseID = _CaseID_
            frmDefendantEntry.ShowDialog()
        Else
            frmDefendantEntry._CaseID = _CaseID
            frmDefendantEntry.ShowDialog()
        End If
    End Sub

    Private Sub AddThirdParty_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddThirdParty.LinkClicked
        If _isViewCase Then
            frmThirdPartyEntry._CaseID = _CaseID_
            frmThirdPartyEntry.ShowDialog()
        Else
            frmThirdPartyEntry._CaseID = _CaseID
            frmThirdPartyEntry.ShowDialog()
        End If
    End Sub

    Private Sub AddLawyerPlaintiff_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddLawyerPlaintiff.LinkClicked
        If _isViewCase Then
            frmLawyerPlaintiff._CaseID = _CaseID_
            frmLawyerPlaintiff.ShowDialog()
        Else
            frmLawyerPlaintiff._CaseID = _CaseID
            frmLawyerPlaintiff.ShowDialog()
        End If
    End Sub

    Private Sub AddLawyerDefendant_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddLawyerDefendant.LinkClicked
        If _isViewCase Then
            frmLawyerDefendant._CaseID = _CaseID_
            frmLawyerDefendant.ShowDialog()
        Else
            frmLawyerDefendant._CaseID = _CaseID
            frmLawyerDefendant.ShowDialog()
        End If
    End Sub

    Private Sub AddLawyerThirdParty_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddLawyerThirdParty.LinkClicked
        If _isViewCase Then
            frmLawyerThirdParty._CaseID = _CaseID_
            frmLawyerThirdParty.ShowDialog()
        Else
            frmLawyerThirdParty._CaseID = _CaseID
            frmLawyerThirdParty.ShowDialog()
        End If
    End Sub

    Private Sub AddFilePlaintiff_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddFilePlaintiff.LinkClicked
        If _isViewCase Then
            frmAttachFile._CaseID = _CaseID_
            frmAttachFile.isWhere(False, True, False, False)
            frmAttachFile.ShowDialog()
        Else
            frmAttachFile._CaseID = _CaseID
            frmAttachFile.isWhere(False, True, False, False)
            frmAttachFile.ShowDialog()
        End If
    End Sub

    Private Sub AddFileDefendant_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddFileDefendant.LinkClicked
        If _isViewCase Then
            frmAttachFile._CaseID = _CaseID_
            frmAttachFile.isWhere(False, False, True, False)
            frmAttachFile.ShowDialog()
        Else
            frmAttachFile._CaseID = _CaseID
            frmAttachFile.isWhere(False, False, True, False)
            frmAttachFile.ShowDialog()
        End If
    End Sub

    Private Sub AddFileThirdParty_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddFileThirdParty.LinkClicked
        If _isViewCase Then
            frmAttachFile._CaseID = _CaseID_
            frmAttachFile.isWhere(False, False, False, True)
            frmAttachFile.ShowDialog()
        Else
            frmAttachFile._CaseID = _CaseID
            frmAttachFile.isWhere(False, False, False, True)
            frmAttachFile.ShowDialog()
        End If
    End Sub

    Private Sub AddDescriptionPlaintiff_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddDescriptionPlaintiff.LinkClicked
        If _isViewCase Then
            frmDescription._CaseID = _CaseID_
            frmDescription.isWhere(False, True, False, False)
            frmDescription.ShowDialog()
        Else
            frmDescription._CaseID = _CaseID
            frmDescription.isWhere(False, True, False, False)
            frmDescription.ShowDialog()
        End If
    End Sub

    Private Sub AddDescriptionDefendant_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddDescriptionDefendant.LinkClicked
        If _isViewCase Then
            frmDescription._CaseID = _CaseID_
            frmDescription.isWhere(False, False, True, False)
            frmDescription.ShowDialog()
        Else
            frmDescription._CaseID = _CaseID
            frmDescription.isWhere(False, False, True, False)
            frmDescription.ShowDialog()
        End If
    End Sub

    Private Sub AddDescriptionThirdParty_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddDescriptionThirdParty.LinkClicked
        If _isViewCase Then
            frmDescription._CaseID = _CaseID_
            frmDescription.isWhere(False, False, False, True)
            frmDescription.ShowDialog()
        Else
            frmDescription._CaseID = _CaseID
            frmDescription.isWhere(False, False, False, True)
            frmDescription.ShowDialog()
        End If
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If Application.OpenForms().OfType(Of frmCase).Any Then
            Close()
        Else
            ShowForm(frmCase)
        End If
    End Sub

    Private Sub ShowForm(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        Me.Close()
        frm.Show()
        frm.BringToFront()
    End Sub

    Private Sub dgvPlaintiff_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvPlaintiff.DoubleClick
        frmViewClient._isPlaintiff = True
        frmViewClient._isDefendant = False
        frmViewClient._isThirdParty = False
        If _isViewCase Then
            frmViewClient._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewClient._CaseID = _CaseID
        Else
            frmViewClient._CaseID = _CaseID
        End If
        frmViewClient._PlaintiffID = dgvPlaintiff.CurrentRow.Cells(0).Value
        frmViewClient.ShowDialog()
    End Sub

    Private Sub dgvDefendant_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDefendant.DoubleClick
        frmViewClient._isPlaintiff = False
        frmViewClient._isDefendant = True
        frmViewClient._isThirdParty = False
        If _isViewCase Then
            frmViewClient._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewClient._CaseID = _CaseID
        Else
            frmViewClient._CaseID = _CaseID
        End If
        frmViewClient._DefendantID = dgvDefendant.CurrentRow.Cells(0).Value
        frmViewClient.ShowDialog()
    End Sub

    Private Sub dgvThirdParty_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvThirdParty.DoubleClick
        frmViewClient._isPlaintiff = False
        frmViewClient._isDefendant = False
        frmViewClient._isThirdParty = True
        If _isViewCase Then
            frmViewClient._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewClient._CaseID = _CaseID
        Else
            frmViewClient._CaseID = _CaseID
        End If
        frmViewClient._ThirdPartyID = dgvThirdParty.CurrentRow.Cells(0).Value
        frmViewClient.ShowDialog()
    End Sub

    Private Sub dgvJudge_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvJudge.DoubleClick
        frmViewPersonInfo._isJudge = True
        frmViewPersonInfo._isCourtClerk = False
        frmViewPersonInfo._isLawyerPlaintiff = False
        frmViewPersonInfo._isLawyerDefendant = False
        frmViewPersonInfo._isLawyerThirdParty = False
        If _isViewCase Then
            frmViewPersonInfo._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewPersonInfo._CaseID = _CaseID
        Else
            frmViewPersonInfo._CaseID = _CaseID
        End If
        frmViewPersonInfo._JudgeID = dgvJudge.CurrentRow.Cells(0).Value
        frmViewPersonInfo.ShowDialog()
    End Sub

    Private Sub dgvCourtClerk_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvCourtClerk.DoubleClick
        frmViewPersonInfo._isJudge = False
        frmViewPersonInfo._isCourtClerk = True
        frmViewPersonInfo._isLawyerPlaintiff = False
        frmViewPersonInfo._isLawyerDefendant = False
        frmViewPersonInfo._isLawyerThirdParty = False
        If _isViewCase Then
            frmViewPersonInfo._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewPersonInfo._CaseID = _CaseID
        Else
            frmViewPersonInfo._CaseID = _CaseID
        End If
        frmViewPersonInfo._CourtClerkID = dgvCourtClerk.CurrentRow.Cells(0).Value
        frmViewPersonInfo.ShowDialog()
    End Sub

    Private Sub dgvLawyerPlaintiff_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLawyerPlaintiff.DoubleClick
        frmViewPersonInfo._isJudge = False
        frmViewPersonInfo._isCourtClerk = False
        frmViewPersonInfo._isLawyerPlaintiff = True
        frmViewPersonInfo._isLawyerDefendant = False
        frmViewPersonInfo._isLawyerThirdParty = False
        If _isViewCase Then
            frmViewPersonInfo._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewPersonInfo._CaseID = _CaseID
        Else
            frmViewPersonInfo._CaseID = _CaseID
        End If
        frmViewPersonInfo._LawyerPlaintiffID = dgvLawyerPlaintiff.CurrentRow.Cells(0).Value
        frmViewPersonInfo.ShowDialog()
    End Sub

    Private Sub dgvLawyerDefendant_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLawyerDefendant.DoubleClick
        frmViewPersonInfo._isJudge = False
        frmViewPersonInfo._isCourtClerk = False
        frmViewPersonInfo._isLawyerPlaintiff = False
        frmViewPersonInfo._isLawyerDefendant = True
        frmViewPersonInfo._isLawyerThirdParty = False
        If _isViewCase Then
            frmViewPersonInfo._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewPersonInfo._CaseID = _CaseID
        Else
            frmViewPersonInfo._CaseID = _CaseID
        End If
        frmViewPersonInfo._LawyerDefendantID = dgvLawyerDefendant.CurrentRow.Cells(0).Value
        frmViewPersonInfo.ShowDialog()
    End Sub

    Private Sub dgvLawyerThirdParty_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLawyerThirdParty.DoubleClick
        frmViewPersonInfo._isJudge = False
        frmViewPersonInfo._isCourtClerk = False
        frmViewPersonInfo._isLawyerPlaintiff = False
        frmViewPersonInfo._isLawyerDefendant = False
        frmViewPersonInfo._isLawyerThirdParty = True
        If _isViewCase Then
            frmViewPersonInfo._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewPersonInfo._CaseID = _CaseID
        Else
            frmViewPersonInfo._CaseID = _CaseID
        End If
        frmViewPersonInfo._LawyerThirdPartyID = dgvLawyerThirdParty.CurrentRow.Cells(0).Value
        frmViewPersonInfo.ShowDialog()
    End Sub

    Private Sub dgvFilePlaintiff_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvFilePlaintiff.DoubleClick
        frmViewImageCase._isFileCasePlaintiffID = True
        frmViewImageCase._isFileCaseDefendantID = False
        frmViewImageCase._isFileCaseThirdPartyID = False
        If _isViewCase Then
            frmViewImageCase._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewImageCase._CaseID = _CaseID
        Else
            frmViewImageCase._CaseID = _CaseID
        End If
        frmViewImageCase._FileCasePlaintiffID = dgvFilePlaintiff.CurrentRow.Cells(0).Value
        frmViewImageCase.ShowDialog()
    End Sub

    Private Sub dgvFileDefendant_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvFileDefendant.DoubleClick
        frmViewImageCase._isFileCasePlaintiffID = False
        frmViewImageCase._isFileCaseDefendantID = True
        frmViewImageCase._isFileCaseThirdPartyID = False
        If _isViewCase Then
            frmViewImageCase._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewImageCase._CaseID = _CaseID
        Else
            frmViewImageCase._CaseID = _CaseID
        End If
        frmViewImageCase._FileCaseDefendantID = dgvFileDefendant.CurrentRow.Cells(0).Value
        frmViewImageCase.ShowDialog()
    End Sub

    Private Sub dgvFileThirdParty_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvFileThirdParty.DoubleClick
        frmViewImageCase._isFileCasePlaintiffID = False
        frmViewImageCase._isFileCaseDefendantID = False
        frmViewImageCase._isFileCaseThirdPartyID = True
        If _isViewCase Then
            frmViewImageCase._CaseID = _CaseID_
        ElseIf _isAddCase Then
            frmViewImageCase._CaseID = _CaseID
        Else
            frmViewImageCase._CaseID = _CaseID
        End If
        frmViewImageCase._FileCaseThirdPartyID = dgvFileThirdParty.CurrentRow.Cells(0).Value
        frmViewImageCase.ShowDialog()
    End Sub

    Private Sub dgvDescriptionPlaintiff_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDescriptionPlaintiff.DoubleClick
        frmViewDescriptionCase._isPalintiff = True
        frmViewDescriptionCase._isDefendant = False
        frmViewDescriptionCase._isThirdParty = False
        If _isViewCase Then
            frmViewDescriptionCase._Case = _CaseID_
        ElseIf _isAddCase Then
            frmViewDescriptionCase._Case = _CaseID
        Else
            frmViewDescriptionCase._Case = _CaseID
        End If
        frmViewDescriptionCase._DescriptionCasePlaintiffID = dgvDescriptionPlaintiff.CurrentRow.Cells(0).Value
        frmViewDescriptionCase.ShowDialog()
    End Sub

    Private Sub dgvDescriptionDefendant_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDescriptionDefendant.DoubleClick
        frmViewDescriptionCase._isPalintiff = False
        frmViewDescriptionCase._isDefendant = True
        frmViewDescriptionCase._isThirdParty = False
        If _isViewCase Then
            frmViewDescriptionCase._Case = _CaseID_
        ElseIf _isAddCase Then
            frmViewDescriptionCase._Case = _CaseID
        Else
            frmViewDescriptionCase._Case = _CaseID
        End If
        frmViewDescriptionCase._DescriptionCaseDefendantID = dgvDescriptionDefendant.CurrentRow.Cells(0).Value
        frmViewDescriptionCase.ShowDialog()
    End Sub

    Private Sub dgvDescriptionThirdParty_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDescriptionThirdParty.DoubleClick
        frmViewDescriptionCase._isPalintiff = False
        frmViewDescriptionCase._isDefendant = False
        frmViewDescriptionCase._isThirdParty = True
        If _isViewCase Then
            frmViewDescriptionCase._Case = _CaseID_
        ElseIf _isAddCase Then
            frmViewDescriptionCase._Case = _CaseID
        Else
            frmViewDescriptionCase._Case = _CaseID
        End If
        frmViewDescriptionCase._DescriptionCaseThirdPartyID = dgvDescriptionThirdParty.CurrentRow.Cells(0).Value
        frmViewDescriptionCase.ShowDialog()
    End Sub

    Private Sub btnFinishCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinishCase.Click
        update_Case_Finish()
    End Sub

    Protected Sub update_Case_Finish()
        If _isViewCase Then
            Dim oCase As New cCase
            If MessageBox.Show("តើអ្នកប្រាកជាចង់បញ្ចប់សំណុំរឿងនេះមែនទេ?", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If oCase.update_Case_Finish(_CaseID_, CInt(txtTotalDays.Text)) Then
                    EnableCase_All()
                    MessageBox.Show("បញ្ចប់រឿងក្តីបានសម្រេច!", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("បញ្ចប់រឿងក្តីមិនបានសម្រេចទេ!", "បញ្ចប់សំណុំរឿង", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        End If
    End Sub
End Class